package com.weiyuwqj.admin.controller.admin.che;

//import com.weiyuwqj.admin.controller.che300.PriceResult;
//import com.weiyuwqj.admin.controller.che300.CheApi;
import com.weiyuwqj.admin.controller.base.BaseController;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.util.HttpUtils;
import com.weiyuwqj.api.util.JsonUtils;
import com.weiyuwqj.api.web.WebException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fuxingxing on 16/6/24.
 */
@Controller
public class CheController extends BaseController {


    String token="000a69a6552f1a45cb2aaf14aced3395";

    @ResponseBody
    @RequestMapping(value = "/admin/getAllCity")
    public String getAllCity() throws  Exception{

        Map param=new HashMap();
        param.put("token",token);

        String jsonStr = HttpUtils.URLGet("http://api.che300.com/service/getAllCity", param, "UTF-8");

        Map map= JsonUtils.json2Map(jsonStr);

        Object list=map.get("city_list");
        if(list!=null && list instanceof  List){
          /*  List mapList=(List)list;
            List<SysCity> cityList=new ArrayList<>();
            for(int i=0;i<mapList.size();i++ ){
                SysCity city=new SysCity();
               // city.setCityId();
            }
            //List<SysCity> cityList=(List<SysCity>)list;
            cheService.insert(cityList);*/
        }

        return jsonStr;
    }
    //品牌
    @ResponseBody
    @RequestMapping(value = "/admin/getCarBrandList")
    public Object getCarBrandList() throws  Exception{
        //车三百接口
        List<Map> retList=new ArrayList<Map>();
        ServletContext context=getServletContext();

        Object object=context.getAttribute("brandList");

        if(object!=null){
            retList=(List)object;
        }else{
            String url="http://api.che300.com/service/getCarBrandList";

            Map param=new HashMap();
            param.put("token",token);

            String jsonStr = HttpUtils.URLGet(url, param, "UTF-8");

            Map map=JsonUtils.json2Map(jsonStr);

            List list=(List)map.get("brand_list");

            Map<String,BrandGroup> dataMap=new HashMap<String,BrandGroup>();

            for(int i=0;i<list.size();i++){
                Map item=(Map)list.get(i);
                String initial=(String)item.get("initial");

                if(!dataMap.containsKey(initial)){
                    dataMap.put(initial,new BrandGroup(initial));
                }
                BrandGroup  b=dataMap.get(initial);;
                String id=(String)item.get("brand_id");
                String name=(String)item.get("brand_name");
                Brand brand=new Brand(id,name);
                b.addBrand(brand);
            }



            for (Map.Entry<String, BrandGroup> entry : dataMap.entrySet()) {
                Map<String,Object>  m=new HashMap<String,Object>();
                m.put("initial",entry.getKey());
                m.put("list",entry.getValue().getList());
                retList.add(m);
            }
            context.setAttribute("brandList",retList);
        }


        return retList;


    }





    @ResponseBody
    @RequestMapping(value = "/admin/getCarSeriesList")
    public Object getCarSeriesList(@RequestParam(defaultValue = "0")Integer brandId) throws  Exception{

        String url="http://api.che300.com/service/getCarSeriesList";

        Map param=new HashMap();
        param.put("token",token);
        param.put("brandId",brandId+"");

        String jsonStr = HttpUtils.URLGet(url, param, "UTF-8");

        Map map=JsonUtils.json2Map(jsonStr);



        return map.get("series_list");
    }

    @ResponseBody
    @RequestMapping(value = "/admin/getCarModelList")
    public Object getCarModelList(@RequestParam(defaultValue = "0")Integer seriesId) throws  Exception{

        String url="http://api.che300.com/service/getCarModelList";

        Map param=new HashMap();
        param.put("token",token);
        param.put("seriesId",seriesId+"");

        String jsonStr = HttpUtils.URLGet(url, param, "UTF-8");

        Map map=JsonUtils.json2Map(jsonStr);



        return map.get("model_list");
    }



    class Brand{
        private String brand_id;
        private String brand_name;
        private String logo_path;

        public Brand(String brand_id,String brand_name) {
            this.brand_name = brand_name;
            this.brand_id = brand_id;
        }

        public String getBrand_id() {
            return brand_id;
        }

        public void setBrand_id(String brand_id) {
            this.brand_id = brand_id;
        }

        public String getBrand_name() {
            return brand_name;
        }

        public void setBrand_name(String brand_name) {
            this.brand_name = brand_name;
        }

        public String getLogo_path() {
            return logo_path;
        }

        public void setLogo_path(String logo_path) {
            this.logo_path = logo_path;
        }
    }

    class BrandGroup {
        private String initial;
        private  List<Brand> list;

        public BrandGroup(String initial) {
            this.initial = initial;
            this.list=new ArrayList<Brand>();
        }

        public List getList() {
            return list;
        }

        public void setList(List list) {
            this.list = list;
        }

        public String getInitial() {
            return initial;
        }

        public void setInitial(String initial) {
            this.initial = initial;
        }
        public void addBrand(Brand brand){
            list.add(brand);
        }
    }

//    //车辆估价
//    @ResponseBody
//    @RequestMapping(value="/admin/queryPrice" )
//    public  Object queryPrice(String modelId,String regDate,String mile){
//        String token="000a69a6552f1a45cb2aaf14aced3395";
//        Map param=new HashMap();
//        param.put("token",token);
//        param.put("modelId",modelId);//车型号
//        param.put("regDate",regDate+"-7");//注册日期
//        param.put("mile",mile);//里程数
//        param.put("zone",12+"");
//        CheApi api= CheApi.getInstance();
//        PriceResult priceResult=api.getPrice(param);
//        Map  ret= Message.RESULT;
//        if(priceResult==null||priceResult.getStatus()!=1){
//            throw new WebException("评估参数错误");
//        }
//        ret.put("result","success");
//        double price=Double.parseDouble(priceResult.getEval_price())*10000;
//        int val=(int)price;
//        val=(val/10000)*10000;
//        getSession().setAttribute("price", val);
//        ret.put("price",val);
//        return ret;
//    }

}
